package com.eshop.dao;

import com.eshop.domain.User;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * Created by Sergey on 17.12.13.
 */
public class UserDao extends HibernateUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(UserDao.class);

    public List<User> load() {
        Session session = null;
        try {
            session = getSessionFactory().openSession();
            session.beginTransaction();
            session.load(User.class, null);
            session.getTransaction().commit();
        } catch (Exception e) {
            LOGGER.warn(e.getMessage());
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return null;
    }

    public User getById(int id) {
        Session session = null;
        try {
            session = getSessionFactory().openSession();
            session.beginTransaction();
            session.get(User.class, id);
            session.getTransaction().commit();
        } catch (Exception e) {
            LOGGER.warn(e.getMessage());
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return null;
    }
}
